package com.okilo.online.app.tools.helper

import com.okilo.online.app.OkiloApplication
import com.okilo.online.app.configs.OkiloConfigs
import com.okilo.online.app.configs.OkiloConfigs.addParams
import com.okilo.online.app.event.OkiloEventData
import com.okilo.online.app.request.HttpHelper
import com.okilo.online.app.request.IHttpCallback
import com.okilo.online.app.tools.shared.OkiloSharedHp
import com.okilo.online.app.tools.shared.OkiloSharedKey
import org.json.JSONObject

/**
 * OkiloLoan
 * @Author wwdeng
 * @CreateTime: 2024/11/20 18:06
 * @Annotation：保存自定义数据接口 参数
 */
object OkiloCustomDataHp {

    /**
     * userName                         : 用户名
     * userAvatarUrl                    ： 用户头像链接
     */

    /**
     * 获取保存的自定义数据
     */
    fun gOkiloAppCustomData(): JSONObject {
        val customData = OkiloSharedHp.gOkiloSharedString(OkiloSharedKey.SP_OKILO_CUSTOM_DATA)
        if (customData.isEmpty()){
            return JSONObject()
        }
        return JSONObject(customData)
    }

    /**
     * 获取用户昵称
     */
    fun gOkiloAppCustomUserName(): String{
        val data = gOkiloAppCustomData()
        return data.optString("userName","")
    }

    /**
     * 获取用户头像地址
     */
    fun gOkiloAppCustomUserAvatarUrl(): String{
        val data = gOkiloAppCustomData()
        return data.optString("userAvatarUrl","")
    }

    /**
     * 保存用户昵称
     */
    fun sOkiloAppCustomUserNameAction(userName: String){
        val obj = gOkiloAppCustomData()
        obj.put("userName",userName)
        sOkiloAppCustomDataAction(obj)
    }

    /**
     * 保存用户头像id
     */
    fun sOkiloAppCustomUserAvatar(imgUrl: String){
        val obj = gOkiloAppCustomData()
        obj.put("userAvatarUrl",imgUrl)
        sOkiloAppCustomDataAction(obj)
    }

    /**
     * 获取自定义数据
     */
    fun gOkiloAppCustomDataAction(){
        val httpUrl = OkiloConfigs.gOkiloRequestUrl()
        val params = JSONObject().addParams().apply {
            put("path", OkiloConfigs.gOkiloRequestPath("GET_CUSTOM_DATA"))
        }
        HttpHelper.post(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                when(data.optInt("code")) {
                    0 -> {//成功
                        val obj = data.optJSONObject("obj")
                        if (obj != null && obj.has("userInfo")){
                            val userInfo = obj.optString("userInfo")
                            //保存自定义数据
                            OkiloSharedHp.sOkiloSharedData(OkiloSharedKey.SP_OKILO_CUSTOM_DATA,userInfo.toString())
                        }
                        //通知自定义数据刷新完成
                        OkiloApplication.app.publisher.publish(OkiloEventData(OkiloEventData.TYPE.OKILO_CUSTOM_DATA_REFRESHED))
                    }
                    else -> {
                    }
                }
            }
            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
            }
        })
    }

    /**
     * 保存数据
     */
    private fun sOkiloAppCustomDataAction(obj: JSONObject, callback: IHttpCallback? = null){
        val httpUrl = OkiloConfigs.gOkiloRequestUrl()
        val params = JSONObject().addParams().apply {
            put("path", OkiloConfigs.gOkiloRequestPath("SAVE_CUSTOM_DATA"))
            put("userInfo",obj.toString())
        }
        HttpHelper.post(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                callback?.onRequestSuccess(data)
                when(data.optInt("code")) {
                    0 -> {//成功
                        val jsonObj = data.optJSONObject("obj")
                        if (jsonObj != null){
                            //保存成功 再更新数据
                            gOkiloAppCustomDataAction()
                        }
                    }
                }
            }

            override fun onRequestError(error: JSONObject) {
                callback?.onRequestError(error)
            }

            override fun onRequestFinish() {
                callback?.onRequestFinish()
            }
        })
    }
}